﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FF.Entity;
using FF.Method;

namespace FF
{
    public partial class FormMain : Form
    {
        private User user;
        public FormMain()
        {
            InitializeComponent();
        }

        public FormMain(User user)
        {
            this.user = user;
            InitializeComponent();
            toolStripStatusLabelUser.Text = "当前用户：" + user.UserName;
        }

        private void 添加用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormUserAdd adduser = new FormUserAdd();
            adduser.Owner = this;
            adduser.ShowDialog();
        }

        private void 查询用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormUserManager form = new FormUserManager();
            form.currentUser = this.user;
            form.Owner = this;
            form.ShowDialog();
        }

        private void 用户类型管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormTypeManager frm = new FormTypeManager(FormTypeManager.FORM_TYPE_USER);
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void 商品管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGood frm = new FormGood(user, FormGood.ACTION_TYPE_ADD);
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.toolStripStatusLabelTime.Text = "当前时间：" + DateTime.Now.ToString();
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            if(!user.Type.Equals(UserTypeMethod.TYPE_ADMIN))
            {
                ToolStripItem[] tsuser = menuStrip1.Items.Find("用户管理ToolStripMenuItem", true);
                tsuser[0].Visible = false;
                ToolStripItem[] tsusertype = menuStrip1.Items.Find("用户类型管理ToolStripMenuItem", true);
                tsusertype[0].Visible = false;
                ToolStripItem[] tscustomertype = menuStrip1.Items.Find("客户类型管理ToolStripMenuItem", true);
                tscustomertype[0].Visible = false;
                
            }
            
        }

        private void 商品查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormGoodManager frm = new FormGoodManager(user == null ? "debug" : user.UserId.ToString());
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void 客户类型管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormTypeManager frm = new FormTypeManager(FormTypeManager.FORM_TYPE_CUSTOMER);
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void 添加客户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormCustomer frm = new FormCustomer(FormCustomer.ACTION_TYPE_ADD);
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void 查询客户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormCustomerManager frm = new FormCustomerManager();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void 销售信息设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormSale frm = new FormSale(this.user,FormSale.ACTION_TYPE_ADD);
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void 销售信息查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormSaleManager frm = new FormSaleManager(this.user);
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void 销售统计ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormSaleReport frm = new FormSaleReport();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void toolStripMenuItemDatabaseBackup_Click(object sender, EventArgs e)
        {
            String sourcePath = Application.StartupPath + "\\db\\ff.db";
            DateTime now = DateTime.Now;
            String targetPath = Application.StartupPath + "\\db\\ff.db." + now.Year.ToString().PadLeft(2) + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + "-" + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0'); 

            bool isrewrite = true; // true=覆盖已存在的同名文件,false则反之
            

            try
            {
                System.IO.File.Copy(sourcePath, targetPath, isrewrite);
                MessageBox.Show("备份成功！备份文件保存为" + targetPath);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("备份失败！" + ex.Message);
            }
            
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormAbout frm = new FormAbout();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void 添加退货单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormCancleSale frm = new FormCancleSale(this.user);
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void 退货查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormCancelSaleManager frm = new FormCancelSaleManager();
            frm.Owner = this;
            frm.ShowDialog();
        }
    }
}
